package com.zfast.config.service.Impl;

import com.zfast.config.entity.Menu;
import com.zfast.config.mapper.TreeMenuMapper;
import com.zfast.config.repository.MenuRepository;
import com.zfast.config.service.TreeMenuService;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname TreeMenuServiceImpl
 * @Description None
 * @Date 2019/7/16 23:08
 * @Created by WDD
 */
@Service
public class TreeMenuServiceImpl implements TreeMenuService {

    @Resource
    private TreeMenuMapper treeMenuMapper;

    @Resource
    MenuRepository menuRepository;

    @Override
    public List<Menu> selectByAdminId(String id) {
        //获取用户所有的全部权限(父,子权限)菜单
        List<Menu> menuList = treeMenuMapper.selectByAdminId(id);
        //保存所有的父(主)菜单
        List<Menu> root = new ArrayList<>();
        //遍历所有菜单集合,如果是主菜单的话直接放入root集合
        for (Menu menu : menuList) {
            //pid为0,则为父(主)菜单
            if (menu.getPid().equals("-1")) {
                root.add(menu);
            }
        }
        //这个是遍历所有主菜单,分别获取所有主菜单的所有子菜单
        for (Menu menu : root) {
            //获取所有子菜单 递归
            List<Menu> childrenList = getchildrenMeun(menu.getId(), menuList);
            //这个是实体类中的子菜单集合
            menu.setChildren(childrenList);
        }
        return root;
    }

    @Override
    public List<Menu> selectAll() {
        return menuRepository.findAll();
    }

    @Override
    public Menu selectById(String id) {
        return menuRepository.findById(id).orElse(null);
    }

    @Override
    public Menu selectByName(String name) {
        Menu menu = Menu.builder().name(name).build();
        Example<Menu> userExample = Example.of(menu);
        return menuRepository.findOne(userExample).orElse(null);
    }

    @Override
    public Menu selectByUrl(String url) {
        return menuRepository.findByUrl(url);
    }

    @Override
    public int editByPermission(Menu menu) {
        menuRepository.save(menu);
        return 1;
    }

    @Override
    public int insertPermission(Menu menu) {
        menuRepository.save(menu);
        return 1;
    }

    @Override
    @Transactional
    public int delByPermissionIds(List<String> ids) {
        treeMenuMapper.delRolePermission(ids);
        menuRepository.deleteAllByIdInBatch(ids);
        return ids.size();
    }

    @Override
    public List<Menu> selectByPid(String id) {
        Menu menu = Menu.builder().pid(id).build();
        Example<Menu> userExample = Example.of(menu);
        return menuRepository.findAll(userExample);
    }

    @Override
    public List<Menu> selectByRoleId(String id) {
        return treeMenuMapper.selectByRoleId(id);
    }

    @Override
    @Transactional
    public int updateRolePermission(List<String> ids, String id) {
        treeMenuMapper.delRolePermissionByRid(id);
        return treeMenuMapper.updateRolePermission(ids, id);
    }


    //递归获取子菜单
    public List<Menu> getchildrenMeun(String id, List<Menu> allMeun) {
        //用于保存子菜单
        List<Menu> childrenList = new ArrayList<>();
        for (Menu info : allMeun) {
            //判断当前的菜单标识是否等于主菜单的id
            if (info.getPid().equals(id)) {
                //如果是的话 就放入主菜单对象的子菜单集合
                childrenList.add(info);
            }
        }

        //这里就是递归了，遍历所有的子菜单
        for (Menu info : childrenList) {
            info.setChildren(getchildrenMeun(info.getId(), allMeun));
        }

        //如果子菜单为空的话，那就说明某菜单下没有子菜单了，直接返回空,子菜单为空的话就不会继续
        //迭代了
        if (childrenList != null && childrenList.size() == 0) {
            return null;
        }
        return childrenList;
    }

}
